import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/community_business/post_detail_page/detail_widget/post_detail_unlock_dialog.dart';
import 'package:tiktokadult/routers/jump_router.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';
import 'package:tiktokadult/hj_utils/date_time_util.dart';

import '../../../../hj_model/video_model.dart';
import '../../../../hj_utils/global.dart';
import '../../../../hj_utils/widget_utils.dart';

class PostResourceInfoWidget extends StatefulWidget {
  final VideoModel videoModel;
  final Function()? callback;

  const PostResourceInfoWidget({
    Key? key,
    required this.videoModel,
    this.callback,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _PostResourceInfoWidgetState();
  }
}

class _PostResourceInfoWidgetState extends State<PostResourceInfoWidget> {
  VideoModel get videoModel => widget.videoModel;

  bool get needLock {
    if (videoModel.freeArea == true) {
      return false;
    }
    if (globalStore.getMe()?.allGoldVideoFree == true && DateTimeUtil.calTime3(globalStore.getMe()?.goldVideoFreeExpire) > 0)
      return false;
    else if (DateTimeUtil.calTime3(globalStore.getMe()?.goldVideoFreeExpire) > 0 && (videoModel.coins ?? 0) < 50) return false;

    return (videoModel.isCoinVideo() && !globalStore.isMe(videoModel.publisher?.uid) && !(videoModel.vidStatus?.hasPaid ?? false));
  }

  String get buyTitleDesc {
    if (needLock) {
      if (videoModel.newsType == 'SEED_LINK') return '${videoModel.realCoins}金币解锁 即可下载种子';
      return '${videoModel.realCoins ?? 0}金币解锁 即可下载种子';
    } else {
      return '开通VIP 即可下载种子';
    }
  }

  _showAlert() async {
    if (needLock) {
      var result = await Get.dialog(PostDetailCoinDialog(model: videoModel));

      if (result != null) {
        videoModel.vidStatus?.hasPaid = true;
        setState(() {});
      }
    } else {
      jumpToWalletPage(tabPosition: 0);
    }
  }

  linkCopyEvent() {
    if (!needLock) {
      Clipboard.setData(ClipboardData(text: videoModel.seedRealLink));
      showToast("链接地址复制成功");
    }
  }

  @override
  Widget build(BuildContext context) {
    if (videoModel.newsType == 'ADULT_GAME' || videoModel.newsType == 'SEED_LINK') {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (videoModel.newsType == 'SEED_LINK') ...[
            if ((videoModel.seedSize ?? 0) > 0)
              Text(
                "[影片大小]：${videoModel.seedSizeDesc}",
                style: TextStyle(
                  color: Color(0xff999999),
                  fontSize: 14,
                  height: 1.5,
                ),
              ),
            if ((videoModel.seedPlayTime ?? 0) > 0)
              Text(
                "[影片时长]：${videoModel.seedPlayTimeDesc}",
                style: TextStyle(
                  color: Color(0xff999999),
                  fontSize: 14,
                  height: 1.5,
                ),
              ),
            Text(
              //"[下载说明]：" + videoModel.seedLinkDesc.trim(),
              "[下载说明]：解锁后请复制种子的下载链接到手机自带浏览器(谷歌/UC/夸克/火狐 等)打开!或使用BT软件(迅雷/Vuze/uTorrent/BitTorrent 等)进行下载.",
              style: TextStyle(
                color: Color(0xff999999),
                fontSize: 14,
                height: 1.5,
              ),
            ),
            Text(
              "[温馨提示]：下载后请勿传播分享,仅供个人欣赏哟~~",
              style: TextStyle(
                color: Color(0xff999999),
                fontSize: 14,
                height: 1.5,
              ),
            ),
            18.sizeBoxH,
          ],
          if (needLock || (!globalStore.isVIP() && videoModel.freeArea != true)) ...[
            Align(
              alignment: Alignment.center,
              child: InkWell(
                onTap: _showAlert,
                child: Text(
                  buyTitleDesc,
                  style: TextStyle(
                    fontSize: 16,
                    color: AppColors.primaryHighColor,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
          ] else ...[
            Align(
              alignment: Alignment.center,
              child: InkWell(
                onTap: linkCopyEvent,
                child: Image.asset('link_copy_icon.png'.communityPath, width: 180, height: 32),
              ),
            )
          ],
        ],
      );
    }
    return SizedBox();
  }
}
